//
//  CustomerDismissingAnimator.swift
//  About me
//
//  Created by Nathan on 4/26/15.
//  Copyright (c) 2015 TAC. All rights reserved.
//

import UIKit
import pop

class CustomerDismissingAnimator: NSObject, UIViewControllerAnimatedTransitioning{
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let toVC = transitionContext.viewController(forKey: .to)
        toVC?.view.tintAdjustmentMode = .normal
        let fromVC = transitionContext.viewController(forKey: .from)
        let subViews = transitionContext.containerView.subviews as NSArray
        let dimmingView = subViews.firstObject as! UIView
        
        let opacityAnimation = POPBasicAnimation(propertyNamed: kPOPLayerOpacity)
        opacityAnimation?.toValue = 0
        let positionAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionX)
        positionAnimation?.toValue = transitionContext.containerView.center.x * 4
        positionAnimation?.springBounciness = 10
        positionAnimation?.completionBlock  = {(anim : POPAnimation?,finished : Bool) in transitionContext.completeTransition(true)}
        
        let scaleAnimation                 = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
        scaleAnimation?.springBounciness    = 20
        scaleAnimation?.toValue             = NSValue(cgPoint: CGPoint(x: 1.2, y: 1.4))
        
        fromVC?.view.layer.pop_add(positionAnimation, forKey: "positionAnimation")
        fromVC?.view.layer.pop_add(scaleAnimation, forKey: "scaleAnimation")
        dimmingView.layer.pop_add(opacityAnimation, forKey: "opacityAnimation")
   
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
}
